<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <!--<script src="websocket.js"></script>-->
</head>
<body>
<input type="text" id="message"/>
<button onclick="sendMessage()">发送消息</button>
<button onclick="closeWebSocket()">关闭socket连接</button>
<div id="content"></div>
<script>
    var websocket=null;
    websocket=new WebSocket("ws://localhost:8080/ws")

    websocket.onerror=function () {
        appendMessage("error")
    }
    websocket.onopen=function (ev) {
        appendMessage("open")
    }

    websocket.onmessage=function (event) {
        appendMessage(event.data)
    }
    websocket.onclose=function (ev) {
        appendMessage("close")
    }

    window.onbeforeunload=function (ev) {
        websocket.close()
    }
    function appendMessage(message) {
        var context=$("#content").html()+"<br/>"+message;
        $("#content").html(context)
    }
    function closeWebSocket() {
        websocket.close()
    }
    function sendMessage() {
        var message=$("#message").val()
        console.log(message);
        websocket.send(message)
    }



</script>
</body>
</html>